
import React from 'react';
import { inject, observer } from 'mobx-react';
import WorkPlanCreate from './workPlanCreate/index';
import { WeaErrorPage, WeaTools, WeaLocaleProvider } from 'ecCom';
import { isEqual } from 'lodash';
const getLabel = WeaLocaleProvider.getLabel;

@inject('createWorkPlanStore')
@observer
class WorkPlanCreateSingle extends React.Component {
  constructor(props) {
    super(props);
    const queryNow = this.props.location.query;
    this.state = {
      type: queryNow.type || 'preview',
      visible: true,
      activeKey: queryNow.activeKey || '1',
      workPlanId: queryNow.workPlanId || '',
      selectedUser: queryNow.selectedUser || '',//下拉默认值-9900为全部
      workPlanTypeOptions: queryNow.workPlanTypeOptions || '',
      crmIDs: queryNow.crmIDs || [],
      description: queryNow.description || '',
      beginDate:queryNow.beginDate||'',
      beginTime:queryNow.beginTime||'',
      endDate:queryNow.endDate||'',
      endTime:queryNow.endTime||'',
      docid:queryNow.docid||'',
      //emobile端日程分享
      isfromchatshare: queryNow.isfromchatshare || 0,
      sharer:queryNow.sharer || 0,
    }
  }

  componentDidMount() {
    const {setParam}=this.props.createWorkPlanStore;
    const { type } = this.props.location.query;
    setParam({type:type || 'preview'})
  }

  componentWillReceiveProps(nextProps) {
    const { type, activeKey, workPlanId, beginDate,beginTime,endDate,endTime, selectedUser, workPlanTypeOptions
      , crmIDs, description,docid } = this.props.location.query;
    const nextQuery = nextProps.location.query;
    let params={};
    if('type' in nextQuery && type !== nextQuery.type){
      params={...params,...{type: nextQuery.type}};
    }
    if('activeKey' in nextQuery && activeKey !== nextQuery.activeKey){
      params={...params,...{activeKey: nextQuery.activeKey}};
    }
    if('workPlanId' in nextQuery && workPlanId !== nextQuery.workPlanId){
      params={...params,...{workPlanId: nextQuery.workPlanId}};
    }
    if('beginDate' in nextQuery && beginDate!==nextQuery.beginDate){
      params={...params,...{beginDate: nextQuery.beginDate}};
    }
    if('beginTime' in nextQuery && beginTime!==nextQuery.beginTime){
      params={...params,...{beginTime: nextQuery.beginTime}};
    }
    if('endDate' in nextQuery && endDate!==nextQuery.endDate){
      params={...params,...{endDate: nextQuery.endDate}};
    }
    if('endTime' in nextQuery && endTime!==nextQuery.endTime){
      params={...params,...{endTime: nextQuery.endTime}};
    }
    if('selectedUser' in nextQuery && selectedUser !== nextQuery.selectedUser){
      params={...params,...{selectedUser: nextQuery.selectedUser}};
    }
    if('workPlanTypeOptions' in nextQuery && !isEqual(workPlanTypeOptions, nextQuery.workPlanTypeOptions)){
      params={...params,...{workPlanTypeOptions: nextQuery.workPlanTypeOptions}};
    }
    if('crmIDs' in nextQuery && !isEqual(crmIDs, nextQuery.crmIDs)){
      params={...params,...{crmIDs: nextQuery.crmIDs}};
    }
    if('docid' in nextQuery && !isEqual(docid, nextQuery.docid)){
      params={...params,...{docid: nextQuery.docid}};
    }
    if('description' in nextQuery && description !== nextQuery.description){
      params={...params,...{description: nextQuery.description}};
    }
    this.setState(params);
  }

  componentWillUnmount() {
    //组件卸载时一般清理一些状态
  }

  close = () => {
    // this.setState({ visible: false, activeKey: '1' });
    //单页情况下 直接关闭该页面
    window.close();
  }

  onlyClose = () => {
    this.setState({ visible: false, activeKey: '1' });
  }

  changeTab = (activeKey) => {
    this.setState({ activeKey });
  }

  callParent = () => {
    window.opener.eventRegister.start('childRequest', {request: 'wo shi parent'}, (data)=> {
      // 父级方法执行成功的回调
      // 关闭当前页面
      window.close()
    },(error)=> {
      // 父级方法执行失败的回调
    })
  }

  getcreateConditionParams = () => {
    let { beginDate,beginTime,endDate,endTime } = this.state;
    return ["0","",beginDate,beginTime,endDate,endTime,"0"];
  }

  render() {
    let { type, visible, workPlanId, beginDate,beginTime,endDate,endTime
      , activeKey, selectedUser, workPlanTypeOptions, crmIDs, description,docid, isfromchatshare, sharer } = this.state;
    const {from, isBtnDown} = this.props.location.query;
    // let createConditionParams = ["0","",beginDate,beginTime,endDate,endTime,"0"]; 
    return (
      <div style={{ height: '100%' }} className='wea-workplan-roomset-single'  onClick = {()=> from && this.callParent()}>
        <WorkPlanCreate ecId={`${this && this.props && this.props.ecId || ''}_WorkPlanCreate@3lg1r4`}
          type={type}//新建为'create',查看为'preview',编辑edit,共享share
          isSingle={true}
          visible={visible}//显隐受控
          doClose={() => { this.close() }}//关闭回调, 保存等按钮会自动调用该回调，可以在这里写一些页面的刷新操作
          onlyClose={() => { this.onlyClose() }}//关闭回调,关闭/取消等按钮会自动调用该回调，只做关闭操作
          workPlanId={workPlanId} //日程id，查看日程时用到
          createConditionParams={this.getcreateConditionParams()}//创建的默认值日期和时间,没有传[]
          activeKey={activeKey} //查看页面显示的面板1是基本信息，7是相关交流
          changeTab={this.changeTab}//查看页面切换面板的回调
          selectUser={selectedUser}//新建日程的接收人id
          workPlanTypeOptions={workPlanTypeOptions}//客户模块的新建日程，普通新建传'',客户新建传3
          crmIDs={crmIDs}//通过客户模块新建日程，并自动带出相关客户,[{'id':'','name':''}]
          docid={docid}
          description={description}//客户模块需求，支持新建时传入的'基本信息-内容'
          isfromchatshare={isfromchatshare}
          sharer={sharer}
          isBtnDown={isBtnDown === 'true'} // 按钮显示的位置
        />
      </div>
    )
  }

}

//组件检错机制
class MyErrorHandler extends React.Component {
  render() {
    const hasErrorMsg = this.props.error && this.props.error !== "";
    return (
      <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@qlrixj`} msg={hasErrorMsg ? this.props.error : getLabel(383324, "对不起，该页面异常，请联系管理员")} />
    );
  }
}

WorkPlanCreateSingle = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(WorkPlanCreateSingle);

export default WorkPlanCreateSingle
